﻿/* ==============================================================================
* 功能描述：RoomMsgHandler
* 创 建 者：青金石
* 创建日期：2020/6/3 17:12:16
* ==============================================================================*/

using TCPServer.script.net;

//处理与房间相关的协议
public partial class MsgHandler
{
    //获取玩家战绩
    public static void MsgGetAchieve(ClientState state, MsgBase msgBase)
    {
        MsgGetAchieve msg = (MsgGetAchieve)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        msg.playerData = player.data;
        msg.id = player.id;
        msg.result = 0;
        player.Send(msg);
    }

    //获取房间列表
    public static void MsgGetRoomList(ClientState state, MsgBase msgBase)
    {
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        player.Send(RoomManager.ToMsg());
    }

    //创建房间
    public static void MsgCreateRoom(ClientState state, MsgBase msgBase)
    {
        MsgCreateRoom msg = (MsgCreateRoom)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        //玩家已经在房间中，不能创建房间
        if (player.roomID >= 0)
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //创建房间
        Room room = RoomManager.AddRoom();
        room.maxCount = msg.maxCount;
        room.AddPlayer(player.id);
        msg.result = 0;
        player.Send(msg);
    }

    //进入房间
    public static void MsgEnterRoom(ClientState state, MsgBase msgBase)
    {
        MsgEnterRoom msg = (MsgEnterRoom)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        //玩家已经进入其它房间
        if (player.roomID >= 0)
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //获取房间
        Room room = RoomManager.GetRoom(msg.id);
        if (room == null)
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //进入房间
        if (!room.AddPlayer(player.id))
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }

        //返回协议
        msg.result = 0;
        player.Send(msg);
    }

    //获取房间信息
    public static void MsgGetRoomInfo(ClientState state, MsgBase msgBase)
    {
        MsgGetRoomInfo msg = (MsgGetRoomInfo)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        //获取房间
        Room room = RoomManager.GetRoom(player.roomID);
        if (room == null)
        {
            player.Send(msg);
            return;
        }
        //发送房间信息
        player.Send(room.ToMsg());
    }

    //离开房间
    public static void MsgLeaveRoom(ClientState state, MsgBase msgBase)
    {
        MsgLeaveRoom msg = (MsgLeaveRoom)msgBase;
        Player player = state.player;
        if (player == null)
        {
            System.Console.WriteLine("Player is empty");
            return;
        }
        Room room = RoomManager.GetRoom(player.roomID);
        if (room == null)
        {
            System.Console.WriteLine("Room is empty");
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //移除玩家，并返回协议
        if (room.RemovePlayer(player.id))
        {
            msg.result = 0;
        }
        else
        {
            msg.result = 1;
        }

        player.Send(msg);
    }

    //收到发送聊天信息协议
    public static void MsgChatSend(ClientState state, MsgBase msgBase)
    {
        MsgChatSend msgSend = (MsgChatSend)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        if (string.IsNullOrEmpty(msgSend.msg))
        {
            msgSend.result = 1;
            player.Send(msgSend);
            return;
        }
        msgSend.result = 0;
        player.Send(msgSend);
        //设置接收消息协议
        MsgChatReceive msgReceive = new MsgChatReceive();
        msgReceive.msg = msgSend.msg;
        msgReceive.sender = player.id;
        msgReceive.senderName = msgSend.senderName;
        //房间列表界面聊天
        if (msgSend.chatEnv == 0)
        {
            foreach (Player p in PlayerManager.players.Values)
            {
                //如果玩家在 大厅，则发送消息
                if (p.roomID < 0)
                {
                    p.Send(msgReceive);
                }
            }
        }
        else//房间内聊天
        {
            Room room = RoomManager.GetRoom(player.roomID);
            if (room == null)
            {
                msgReceive.result = 1;
                return;
            }
            foreach (string id in room.playerIds.Keys)
            {
                Player tPlayer = PlayerManager.GetPlayer(id);
                msgReceive.result = 0;
                tPlayer.Send(msgReceive);
            }
        }
    }

    /// <summary>
    /// 收到开始战斗协议
    /// </summary>
    /// <param name="state"></param>
    /// <param name="msgBase"></param>
    public static void MsgStartBattle(ClientState state, MsgBase msgBase)
    {
        MsgStartBattle msg = (MsgStartBattle)msgBase;
        Player player = state.player;
        if (player == null)
        {
            return;
        }
        Room room = RoomManager.GetRoom(player.roomID);
        if (room == null)
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //房主才能开战
        if (!room.IsOwner(player))
        {
            msg.result = 1;
            player.Send(msg);
            return;
        }
        //开战
        if (!room.StartBattle())
        {
            msg.result = 1;
            player.Send(msg);
            System.Console.WriteLine("start fail");
            return;
        }
        msg.result = 0;
        player.Send(msg);
        System.Console.WriteLine("StartBattleeeeeee!");
    }
}